package com.handinfo.game;

import javax.microedition.lcdui.Graphics;

import com.handinfo.engine.AbstractGameScreen;
import com.handinfo.engine.Actor;
import com.handinfo.engine.Animation;
import com.handinfo.engine.GameManager;
import com.handinfo.engine.resource.Resource;
import com.handinfo.uimenu.UIListener;
import com.handinfo.uimenu.UIManager;
import com.handinfo.uimenu.UIMessageBox;
import com.handinfo.uimenu.UIWindowFactory;
import com.handinfo.util.Tools;

public class ActorNPC extends AbstractGameActor
{
	public ActorNPC()
	{
		super();
	}

	public ActorNPC(Resource resource)
	{
		super(resource);
	}

	// ////////////////////////////////////////
	// NPC基本属性
	// ////////////////////////////////////////

	public long[] m_relatedActorsID;

	// ////////////////////////////////////////
	// NPC非战斗属性
	// ////////////////////////////////////////
	// public NpcCombatSkill m_curCombatSkill;

	// ////////////////////////////////////////
	// NPC战斗属性
	// ////////////////////////////////////////

	public Actor clone(Actor actor)
	{
		ActorNPC ca = new ActorNPC();

		ca.m_scene = actor.m_scene;
		ca.m_anims = new Animation[actor.m_anims.length];
		System.arraycopy(actor.m_anims, 0, ca.m_anims, 0, actor.m_anims.length);
		ca.m_anim = actor.m_anim;
		ca.m_actionIDNow = actor.m_actionIDNow;
		ca.m_actionIDBefore = actor.m_actionIDBefore;
		ca.m_frameId = actor.m_frameId;
		ca.m_frameIndex = actor.m_frameIndex;
		ca.m_frameDuration = actor.m_frameDuration;
		ca.m_actionCycle = actor.m_actionCycle;
		ca.m_actionOver = actor.m_actionOver;
		ca.m_layer = actor.m_layer;
		ca.m_phylayer = actor.m_phylayer;
		ca.m_flag = actor.m_flag;
		ca.m_flipX = actor.m_flipX;
		ca.m_flipY = actor.m_flipY;
		ca.m_visible = actor.m_visible;
		ca.m_enabled = actor.m_enabled;
		ca.m_needDraw = actor.m_needDraw;
		ca.m_posX = actor.m_posX;
		ca.m_posY = actor.m_posY;
		ca.m_vX = actor.m_vX;
		ca.m_vY = actor.m_vY;
		ca.m_collideBox = new short[actor.m_collideBox.length];
		System.arraycopy(actor.m_collideBox, 0, ca.m_collideBox, 0,
				actor.m_collideBox.length);
		ca.m_activeBox = new short[actor.m_activeBox.length];
		System.arraycopy(actor.m_activeBox, 0, ca.m_activeBox, 0,
				actor.m_activeBox.length);
		ca.m_parameters = new short[actor.m_parameters.length];
		System.arraycopy(actor.m_parameters, 0, ca.m_parameters, 0,
				actor.m_parameters.length);
		return ca;
	}

	/** 不显示 */
	public static final byte HEAD_STATE_NONE = 0;
	/** 黄问号 */
	public static final byte HEAD_STATE_YELLOW_QUESTION_MARK = 1;
	/** 蓝问号 */
	public static final byte HEAD_STATE_BLUE_QUESTION_MARK = 2;
	/** 黄叹号 */
	public static final byte HEAD_STATE_YELLOW_EXCLAMATION_MARK = 4;
	/** 蓝叹号 */
	public static final byte HEAD_STATE_BLUE_EXCLAMATION_MARK = 8;
	/** 灰问号 */
	public static final byte HEAD_STATE_GRAY_QUESTION_MARK = 16;

	public static final byte IMG_HS_Y_QUESTION_MARK = 0;
	public static final byte IMG_HS_B_QUESTION_MARK = 1;
	public static final byte IMG_HS_Y_EXCLAMATION_MARK = 2;
	public static final byte IMG_HS_B_EXCLAMATION_MARK = 3;
	public static final byte IMG_HS_G_QUESTION_MARK = 4;
	public static final byte IMG_HS_AMOUNT = 5;

	public synchronized void draw(Graphics g, int camX, int camY)
	{
		switch (m_actionIDNow / 4) {
			case ACTION_DEAD1:
			case ACTION_DEAD2:
				break;
			default:
				g.setClip(0, 0, AbstractGameScreen.SCREEN_WIDTH, AbstractGameScreen.SCREEN_HEIGHT);
				Tools.fillArc(g, GameManager.COLOR_ACTOR_SHADOW, m_posX - camX
						+ m_collideBox[0], m_posY - camY + m_collideBox[1],
						m_collideBox[2] - m_collideBox[0], m_collideBox[3] - m_collideBox[1], 0, 360);
				break;
		}
		super.draw(g, camX, camY);
		if (m_name != null) {
			switch (m_head_state) {
				case HEAD_STATE_YELLOW_QUESTION_MARK:// 1黄问号
					g.drawImage(
							GameManager.getInstance().img_head_status[IMG_HS_Y_QUESTION_MARK],
							m_posX - camX, m_posY - camY + m_activeBox[1] - 17,
							Tools.HCENTER_BOTTOM);
					break;
				case HEAD_STATE_BLUE_QUESTION_MARK:// 2蓝问号
					g.drawImage(
							GameManager.getInstance().img_head_status[IMG_HS_B_QUESTION_MARK],
							m_posX - camX, m_posY - camY + m_activeBox[1] - 17,
							Tools.HCENTER_BOTTOM);
					break;
				case HEAD_STATE_YELLOW_EXCLAMATION_MARK:// 4黄叹号
					g.drawImage(
							GameManager.getInstance().img_head_status[IMG_HS_Y_EXCLAMATION_MARK],
							m_posX - camX, m_posY - camY + m_activeBox[1] - 17,
							Tools.HCENTER_BOTTOM);
					break;
				case HEAD_STATE_BLUE_EXCLAMATION_MARK:// 8蓝叹号
					g.drawImage(
							GameManager.getInstance().img_head_status[IMG_HS_B_EXCLAMATION_MARK],
							m_posX - camX, m_posY - camY + m_activeBox[1] - 17,
							Tools.HCENTER_BOTTOM);
					break;
				case HEAD_STATE_GRAY_QUESTION_MARK:// 16灰问号
					g.drawImage(
							GameManager.getInstance().img_head_status[IMG_HS_G_QUESTION_MARK],
							m_posX - camX, m_posY - camY + m_activeBox[1] - 17,
							Tools.HCENTER_BOTTOM);
					break;
			}

			if (GameManager.getInstance().m_role.m_targetActor != null
					&& GameManager.getInstance().m_role.m_targetActor.m_gameID == m_gameID && m_head_tips != null) {
				if (m_head_state == 0) {
					Tools.drawString(g, 0x1b0b02, 0xffffff, m_head_tips,
							m_posX - camX, m_posY - camY + m_activeBox[1] - AbstractGameScreen.FONT_H - 2, Tools.HCENTER_BOTTOM);
				} else {
					Tools.drawString(g, 0x1b0b02, 0xffffff, m_head_tips,
							m_posX - camX, m_posY - camY + m_activeBox[1] - AbstractGameScreen.FONT_H - 22, Tools.HCENTER_BOTTOM);
				}
			}
			if (m_type == ACTOR_TYPE_NPC) {
				if (m_status != ACTOR_STATUS_NPC_DEAD) {
					if (m_faction == FACTION_FRIEND || m_faction == GameManager.getInstance().m_role.m_faction) {
						Tools.drawString(g, Tools.YELLOW, Tools.BLACK, m_name,
								m_posX - camX, m_posY - camY + m_activeBox[1], Tools.HCENTER_BOTTOM);
						
//						1强化装备图标 2学习图标 3战斗功能图标 4商业图标
//						1是锤子 2是卷轴 3是2把剑 4是元宝
						if (m_function_icon > 0 && m_function_icon < 5) {
							if (m_name != null) {
								g.drawImage(GameScene.s_img_f_icon[m_function_icon - 1],
										m_posX - camX - (AbstractGameScreen.s_font.stringWidth(m_name) >> 1),
										m_posY - camY + m_activeBox[1] + 3, Tools.RIGHT_BOTTOM);
							} else {
								g.drawImage(GameScene.s_img_f_icon[m_function_icon - 1],
										m_posX - camX + (m_activeBox[1] >> 1) - 2, m_posY - camY + m_activeBox[1], Tools.RIGHT_BOTTOM);
							}
						}
					}
					else {
						Tools.drawString(g, Tools.RED, Tools.BLACK, m_name, m_posX - camX, m_posY - camY
								+ m_activeBox[1], Tools.HCENTER_BOTTOM);
					}
				}
			}
			else if (m_type == ACTOR_TYPE_MONSTER) {
				if (m_status != ACTOR_STATUS_NPC_DEAD) {
					Tools.drawString(g, Tools.RED, Tools.BLACK, m_name, m_posX - camX, m_posY - camY + m_activeBox[1],
							Tools.HCENTER_BOTTOM);
				}
			}else if(m_type == ACTOR_TYPE_GATE){
				Tools.drawString(g, Tools.WHITE, Tools.BLACK, m_name, m_posX - camX, m_posY - camY + m_activeBox[1]+GameScreen.FONT_H,
						Tools.HCENTER_BOTTOM);
			}
			else if (m_type == ACTOR_TYPE_GATHERING) {
				Tools.drawString(g, Tools.YELLOW, Tools.BLACK, m_name, m_posX - camX, m_posY - camY + m_activeBox[1],
						Tools.HCENTER_BOTTOM);
			}
			else if (m_type == ACTOR_TYPE_RELIVE) {
				Tools.drawString(g, Tools.YELLOW, Tools.BLACK, m_name, m_posX - camX, m_posY - camY + m_activeBox[1],
						Tools.HCENTER_BOTTOM);
			}
		}
	}

	public void logic()
	{

		super.logic();

		if (m_type == ACTOR_TYPE_GATE) {
			logic_gate();
		} else if (m_type == ACTOR_TYPE_NPC) {
			ActorRole role = GameManager.getInstance().m_role;
			if (role.m_status != AbstractGameActor.ACTOR_STATUS_ROLE_COMBAT) {
				int preX = m_prevPosX - role.m_prevPosX;
				int preY = m_prevPosY - role.m_prevPosY;
				int x = m_posX - role.m_posX;
				int y = m_posY - role.m_posY;
				if (preX * preX + preY * preY > 900 && x * x + y * y <= 900) {
					((GameScene)m_scene).setTargetActor(this);
				}
			}
		} else if (m_type == ACTOR_TYPE_MONSTER && m_status != ACTOR_STATUS_NPC_DEAD) {
			ActorRole role = GameManager.getInstance().m_role;
			if (role.m_status != AbstractGameActor.ACTOR_STATUS_ROLE_COMBAT) {
				int preX = m_prevPosX - role.m_prevPosX;
				int preY = m_prevPosY - role.m_prevPosY;
				int x = m_posX - role.m_posX;
				int y = m_posY - role.m_posY;
				if (preX * preX + preY * preY > 900 && x * x + y * y <= 900
						&& (role.m_targetActor == null
								|| role.m_targetActor.m_type == ACTOR_TYPE_NPC
								|| role.m_targetActor.m_type == ACTOR_TYPE_GATHERING
								|| (role.m_targetActor.m_type == ACTOR_TYPE_ROLE && role.checkTargetFriend(role.m_targetActor)))) {
					((GameScene)m_scene).setTargetActor(this);
				}
			}
		}

		if (m_actionOver) {
			dealTargetDamage(true);
		}
		dealSkillEnd();

		switch (m_actionIDNow / DIRECT_NUM) {
			case ACTION_DEAD1:
			case ACTION_DEAD2:
			case ACTION_STAND:
			case ACTION_MOVE:
				break;
			default:
				if (m_actionOver && !m_actionCycle) {
					setAction(ACTION_STAND * DIRECT_NUM + m_direct, 0, 0, true, false);
				}
				break;
		}
	}
	
	public void dealSkillEnd()
	{
		if (m_curCombatSkill != null
				&& m_actionIDNow / DIRECT_NUM != m_curCombatSkill.m_castSrcActionId) {
			m_curCombatSkill = null;
		}
	}

	public void logic_gate()
	{
		ActorRole role = GameManager.getInstance().m_role;
		if (role == null) {
			return;
		}
		if (role.m_pathfinding_start && !role.m_pathfinding_pause) {
			return;
		}
		if (collide(((GameScene) m_scene).m_controlledActor, true) && !collidePreFrame(((GameScene) m_scene).m_controlledActor, true)) {
			// 弹出切换框
			final UIMessageBox messageBox = (UIMessageBox) UIManager.getInstance().createWindow(UIWindowFactory.WINDOW_MESSAGEBOX);
			messageBox.setMessage("确认去" + m_name + "?");
			messageBox.addListener(new UIListener() {
				public void actionPerformed() {
					if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)
							|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
						GameManager.getInstance().m_game_message.sendChange(m_gameID);
						UIManager.getInstance().removeWindow(messageBox);
					}
					else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
						UIManager.getInstance().removeWindow(messageBox);
					}
				}
			});
			// 设置角色站立
			((GameScene) m_scene).m_controlledActor.stay(GameManager.getInstance().m_role.m_direct);
		}
	}
	/**
	 * 自动寻路后调用 不需要判断是否在寻路中
	 */
	public void findPathGate() {
		if (collide(((GameScene) m_scene).m_controlledActor, true)) {
			// 弹出切换框
//			final UIMessageBox messageBox = (UIMessageBox) UIManager.getInstance().createWindow(UIWindowFactory.WINDOW_MESSAGEBOX);
//			messageBox.setMessage("确认去" + m_name + "?");
//			messageBox.addListener(new UIListener() {
//				public void actionPerformed() {
//					if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)
//							|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
//						GameManager.getInstance().m_game_message.sendChange(m_gameID);
//						UIManager.getInstance().removeWindow(messageBox);
//					}
//					else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
//						UIManager.getInstance().removeWindow(messageBox);
//					}
//				}
//			});
			GameManager.getInstance().m_is_autopath = true;
			GameManager.getInstance().m_game_message.sendChange(m_gameID);
			// 设置角色站立
			((GameScene) m_scene).m_controlledActor.stay(GameManager.getInstance().m_role.m_direct);
		}
	}

	/**
	 * 角色是否能移动
	 * 
	 * @return
	 */
	public boolean canMove()
	{
		boolean result = true;
		int actionID = m_actionIDNow / DIRECT_NUM;
		switch (m_actionIDNow / DIRECT_NUM) {
			case ACTION_ATTACK1:
			case ACTION_ATTACK2:
				if (m_curCombatSkill == null)
					return true;
				if (m_curCombatSkill.m_castSrcActionId == actionID
						&& (m_curCombatSkill.m_type == RoleCombatSkill.TYPE_COMMON || m_curCombatSkill.m_type == RoleCombatSkill.TYPE_SING)) {
					result = false;
				}
				break;
			default:
				break;
		}
		return result;
	}

	/**
	 * 当前动作是否可以被技能打断
	 * 
	 * @return
	 */
	public boolean canBeInterrupt(boolean force)
	{
		int actionID = m_actionIDNow / DIRECT_NUM;
		switch (actionID) {
			case ACTION_MOVE:
			case ACTION_STAND:
				return true;
			case ACTION_ATTACK1:
			case ACTION_ATTACK2:
				if (m_curCombatSkill == null)
					return true;
				// 瞬发法术以及吟唱法术的施法阶段无法打断
				if (m_curCombatSkill.m_castSrcActionId == actionID
						&& (m_curCombatSkill.m_type == CombatSkill.TYPE_COMMON || m_curCombatSkill.m_type == CombatSkill.TYPE_SING)) {
					return false;
				}
				// 通道法术，如果不是强制打断，则不会打断
				else if (m_curCombatSkill.m_castSrcActionId == actionID
						&& m_curCombatSkill.m_type == CombatSkill.TYPE_CONTINUOUS
						&& !force) {
					return false;
				}
				// 吟唱法术的吟唱阶段，如果不是强制打断，则不会打断
				else if (m_curCombatSkill.m_singActionId == actionID
						&& m_curCombatSkill.m_type == CombatSkill.TYPE_SING
						&& !force) {
					return false;
				}
				else {
					return true;
				}
			default:
				return false;
		}
	}

	public void updateMove()
	{
		switch (m_actionIDNow / DIRECT_NUM) {
			default:
				return;
			case ACTION_STAND:
			case ACTION_MOVE:
				break;
		}
		super.updateMove();
	}

	public void dispose()
	{
		super.dispose();
		m_relatedActorsID = null;
	}

	public int doAction(Object data)
	{
		return 0;
	}

	public static final int ACTION_STAND = 0;
	public static final int ACTION_MOVE = 1;
	public static final int ACTION_RESERVE1 = 2;
	public static final int ACTION_HURT1 = 3;
	public static final int ACTION_HURT2 = 4;
	public static final int ACTION_HURT3 = 5;
	public static final int ACTION_DEAD1 = 6;
	public static final int ACTION_DEAD2 = 7;
	public static final int ACTION_ATTACK1 = 8;
	public static final int ACTION_ATTACK2 = 9;
	public static final int ACTION_HEAD = 40;

}
